package com.baichang.taoshunda.dao.impl;

import com.baichang.framework.dao.impl.BaseDaoImpl;
import com.baichang.framework.model.Page;
import com.baichang.taoshunda.dao.WaiterListDao;
import com.baichang.taoshunda.entity.WaiterList;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Repository;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Repository
public class WaiterListDaoImpl extends BaseDaoImpl<WaiterList> implements WaiterListDao {
    /**
     * 根据类型编号获取本地服务列表
     *
     * @param typeId   类型编号
     * @param cityId
     * @param ulat
     * @param ulng
     * @param nowPage
     * @param pageSize @return
     */
    @Override
    public List<Map<String, Object>> getwaiterList(int typeId, int cityId, int areaId, double ulat, double ulng, int nowPage, int pageSize) {
        String sql;
        if (areaId == 0) {
            sql = "SELECT *,ROUND(6378.138 * 2 * ASIN(SQRT(POW(SIN(((" + ulat + ") * PI() / 180 - lat * PI() / 180) / 2),2 " +
                    "  ) + COS((" + ulat + ") * PI() / 180) * COS(lat * PI() / 180) * POW( " +
                    " SIN(((" + ulng + ") * PI() / 180 - lng * PI() / 180) / 2),2 " +
                    "  ))) * 1000) AS juli FROM t_waiter_list  " +
                    " WHERE city_id= " + cityId + " AND `state` IN (2,3) AND waiter_type=? AND status=1 ";
        } else {
            sql = "SELECT *,ROUND(6378.138 * 2 * ASIN(SQRT(POW(SIN(((" + ulat + ") * PI() / 180 - lat * PI() / 180) / 2),2 " +
                    " ) + COS((" + ulat + ") * PI() / 180) * COS(lat * PI() / 180) * POW(" +
                    " SIN(((" + ulng + ") * PI() / 180 - lng * PI() / 180) / 2),2 " +
                    "  ))) * 1000) AS juli FROM t_waiter_list " +
                    "  WHERE city_id= " + cityId + " AND `state` IN (2,3) AND waiter_type=? AND region_id = " + areaId + " AND status=1 ";
        }
        sql += " ORDER BY if_payment DESC,juli ASC ";
        return listForMapList(sql, nowPage, pageSize, typeId);
    }

    /**
     * 查询本地服商家
     *
     * @param businessName
     * @param typeId
     * @param address
     * @param userId
     * @param nowPage
     * @param pageSize     @return
     */
    @Override
    public List<Map<String, Object>> queryWaiterBusinessList(String businessName, Integer typeId, String address, Integer userId, int nowPage, int pageSize) {
        List list = new ArrayList();
        String sql = "SELECT wl.* FROM t_waiter_list wl WHERE wl.state IN (1,2,4) AND wl.status=1";
        if (userId > 0) {
            sql += " AND wl.user_id=?";
            list.add(userId);
        }
        if (StringUtils.isNotBlank(businessName)) {
            sql += " AND wl.waiter_name LIKE ?";
            list.add("%" + businessName + "%");
        }
        if (StringUtils.isNotBlank(address)) {
            sql += " AND wl.waiter_adress LIKE ?";
            list.add("%" + address + "%");
        }
        sql += " ORDER BY wl.created DESC ";
        return listForMapList(sql, nowPage, pageSize, list.toArray());
    }

    /**
     * 修改本地服务商家列表
     *
     * @param map
     * @param id
     * @return
     */
    @Override
    public boolean update(Map map, Integer id) {
        String sql = "id=?";
        return update(sql, map, id) > 0;
    }

    /**
     * 删除
     *
     * @param id
     * @param state
     * @return
     */
    @Override
    public int updateWaiterStatus(Integer id, Integer state) {
        String sql = "UPDATE {table} SET state=? WHERE id=?";
        return update(sql, state, id);
    }

    /**
     * 查询全部商家 ， 用于地图的展示 。 返回经纬度  ， 地址  ， 名称
     *
     * @param cityid
     * @param key
     * @return
     */
    @Override
    public List<Map<String, Object>> findAllCompanyAtlas(int cityid, String key) {
//        StringBuffer stringBuffer = new StringBuffer();
//        stringBuffer.append("SELECT a.`lng`,a.`lat`,a.`company_name`,a.`company_phones`,a.`service_phone`,a.`company_address` ");
//        stringBuffer.append("FROM t_company a JOIN t_user b ON a.`user_id`=b.id WHERE a.`status`=1 AND b.`city_id`=? ");
        String sql = " SELECT * FROM {TABLE} WHERE region_id=?";
        return queryForMapList(sql, cityid);
    }

    /**
     * 查询最新数据的id
     *
     * @return
     */
    @Override
    public Map<String, Object> getwaiterListId() {
        String sql = " SELECT * FROM t_waiter_list ORDER BY created DESC LIMIT 1 ";
        return queryForMap(sql);
    }

    /**
     * 查询服务商家详细
     *
     * @param id
     * @return
     */
    @Override
    public Map<String, Object> queryWaiterDetails(Integer id) {
        String sql = " SELECT wl.*,wt.waiter_name AS typeName,tc.name AS provincesName,ti.name AS cityName,ty.name AS areaName FROM t_waiter_list wl " +
                " INNER JOIN t_waiter_type wt ON wt.id=wl.waiter_type INNER JOIN t_city tc ON tc.id=wl.provinces_id INNER JOIN t_city ti ON ti.id=wl.city_id " +
                " INNER JOIN t_city ty ON ty.id=wl.region_id " +
                " WHERE wl.id = ?";
        return queryForMap(sql, id);
    }

    /**
     * 删除
     *
     * @param id
     * @return
     */
    @Override
    public boolean deleteWaiter(Integer id) {
        String sql = "UPDATE {table} SET status=0 WHERE id=?";
        return update(sql, id) > 0;
    }

    /**
     * @param waiterName
     * @param nowPage
     * @param pageSize
     * @return
     */
    @Override
    public Page queryAuditingBus(String waiterName, int nowPage, int pageSize) {
        String sql = "SELECT wl.*,wt.waiter_name AS waiterNames,ch.payment_time AS payTime,ch.payment_money AS payMoney " +
                "FROM t_waiter_list wl INNER JOIN t_waiter_type wt ON wl.waiter_type=wt.id INNER JOIN t_company_panyment_history ch ON ch.waiter_service_id=wl.id " +
                "WHERE wl.state=1 AND wl.if_payment=1 AND wl.status=1";
        if (!waiterName.equals("")) {
            sql += " AND  wl.waiter_name LIKE '%" + waiterName + "%' ";
        }
        sql += " ORDER BY wl.created DESC";
        return pageForMapList(sql, nowPage, pageSize);
    }

    /**
     * @param waiterName
     * @param address
     * @param typeId
     * @param nowPage
     * @param pageSize   @return
     */
    @Override
    public Page queryWaiterBusiness(String waiterName, String address, Integer typeId, int nowPage, int pageSize) {
        List list = new ArrayList();
        String sql = "SELECT wl.*,wt.waiter_name AS waiterNames FROM t_waiter_list wl " +
                " INNER JOIN t_waiter_type wt ON wl.waiter_type=wt.id WHERE wl.status=1 AND wl.state=2";
        if (!waiterName.equals("")) {
            sql += " AND wl.waiter_name LIKE ?";
            list.add("%" + waiterName + "%");
        }
        if (!address.equals("")) {
            sql += " AND wl.waiter_adress LIKE ?";
            list.add("%" + address + "%");
        }
        if (typeId > 0) {
            sql += " AND wl.waiter_type=?";
            list.add(typeId);
        }
        sql += " ORDER BY wl.created DESC";
        return pageForMapList(sql, nowPage, pageSize, list.toArray());
    }

    /**
     * @param id
     * @param state
     * @param starWaiter
     * @return
     */
    @Override
    public int throughService(Integer id, int state, Integer starWaiter) {
        String sql = "UPDATE {table} SET state=?,star=? WHERE id=?";
        return update(sql, state, starWaiter, id);
    }

    /**
     * @param userId
     * @return
     */
    @Override
    public List<Map<String, Object>> queryAllUserWaiter(Integer userId) {
        String sql = "SELECT * FROM {table} WHERE user_id=? AND status=1 AND if_payment=1";
        return queryForMapList(sql, userId);
    }

    /**
     * @param id
     * @return
     */
    @Override
    public Map<String, Object> getIdInfo(Integer id) {
        String sql = "SELECT wl.* ,wt.waiter_name typeName FROM t_waiter_list wl INNER JOIN t_waiter_type  wt ON wt.id= wl.waiter_type WHERE wl.id=? AND status=1";
        return queryForMap(sql, id);
    }

    /**
     * @param id
     * @param status
     * @return
     */
    @Override
    public int updateStatus(Integer id, Integer status) {
        String sql = "UPDATE {table} SET status=? WHERE id=?";
        return update(sql, status, id);
    }

    @Override
    public int getWaiterCount(Integer id) {
        String sql = " SELECT COUNT(id) FROM t_waiter_list   WHERE waiter_type=? AND `status`=1";
        return queryForInt(sql, id);
    }
}